package reflect;

import reflect.annotation.AutoRunClass;
import reflect.annotation.AutoRunMethod;

import java.lang.reflect.Method;

/**
 * 调用Person类中所有被注解@AutoRunMethod标注的方法
 * 方法调用的次数由注解参数决定
 */
public class Test5 {
    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();
        Method[] methods = cls.getDeclaredMethods();
        for (Method method : methods) {
            //判断方法是否被注解@AutoRunMethod标注了
            if (method.isAnnotationPresent(AutoRunMethod.class)) {
                AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                int value = arm.value();
                System.out.println("执行方法:" + method.getName() + "()"+value+"次");
                for(int i=0;i<value;i++) {
                    method.invoke(obj);
                }
            }
        }
    }
}
